﻿using System;
using System.Data.SqlClient;

namespace FileMonitor.Configuration.Tool
{
    public class Utility
    {
        public static string GetValueFormConnstring(string connStr,string key)
        {
            if (connStr.IndexOf(key, StringComparison.CurrentCultureIgnoreCase) >= 0)
            {
                int startIndex=connStr.IndexOf(key,StringComparison.CurrentCultureIgnoreCase)+key.Length;
                int endIndex=connStr.IndexOf(";",startIndex);

                string value = connStr.Substring(startIndex + 1, endIndex-startIndex-1);
                return value;
            }
            throw new Exception("The key not in Connection string");
        }

        public static bool FindProcedure(string dbName, string procedurename)
        {
            FileUtility.SqlDataBaseExtender db= Program.currentForm.CurrentDBList[dbName];
            try
            {
                SqlCommand cmd = new SqlCommand(procedurename);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                db.DiscoverParameters(cmd);
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }


        public static bool FindTable(string dbName, string tablename)
        {
            FileUtility.SqlDataBaseExtender db = Program.currentForm.CurrentDBList[dbName];
            try
            {
                SqlCommand cmd = new SqlCommand(tablename);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                db.DiscoverParameters(cmd);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }




            
    }
}
